library SelectRMCommand initializer init requires LegacySystem, PlayerSelectsHeroCommand, LegacyHumanAbilities


	private function Trig_Player_Selects_Rifleman_Conditions takes nothing returns boolean
		return GetUnitTypeId(GetTriggerUnit()) == 'Hpal'
	endfunction

	private function Trig_Player_Selects_Rifleman_Actions takes nothing returns nothing

		local unit u = GetTriggerUnit()
		local player p = GetOwningPlayer(u)
		local integer id = GetConvertedPlayerId(p)

		call putUnit(p,"hero", u)

		call setPlayerDataInteger(p,"pistol_ammo",15)
		call setPlayerDataInteger(p,"pistol_type",2)
		call setPlayerDataBoolean(p,"pistol_reload",false)

		call SetUnitUserData(u,50)

		set udg_HeroAvailable[1] = udg_HeroAvailable[1] - 1
		set udg_PlayerIcon[id] = "ReplaceableTextures\\CommandButtons\\BTN_Unit_Grunt.blp"
		//call UnitAddItemById(u,'I00V')
		call UnitAddItemById(u,'I006')
		call UnitAddItemById(u,'I006')
		
		set udg_ReloadTime[id] = 5.0
	   
		call SetUnitAbilityLevel(u,'A000', 2) // sprint level 2 for RM/UL/DO
		call AddPrimaryAmmo(u, 10, 50)
		call RegisterAmmoAutoReloadListener(u)
		call RegisterAmmoEmptyListener(u)
		call InitHero(u)
		
		if GetI("player mos", id) == 1 then
			call ModifyHeroSkillPoints( u, bj_MODIFYMETHOD_ADD, 1 )
		endif
		
		set u = null
		set p = null
		
		if( udg_HeroAvailable[1] <= 0 ) then
			call DisableTrigger(GetTriggeringTrigger())
		endif

	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger(  )
		call TriggerRegisterEnterRectSimple( t, GetPlayableMapRect() )
		call TriggerAddCondition( t, Condition( function Trig_Player_Selects_Rifleman_Conditions ) )
		call TriggerAddAction( t, function Trig_Player_Selects_Rifleman_Actions )
	endfunction

endlibrary